import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Button, Input, message, Select, Space, Tag, Table, } from "antd";
import RowEditor from "./RowEditor";
import TableEditor from "./TableEditor";
import ColumnEditor from "./ColumnEditor";
import React, { useEffect, useRef } from "react";
import { trim } from "../../Tools";
import { Popover } from "devextreme-react/popover";
import SelectBox from "devextreme-react/select-box";
import CpasIcon from "~/public/cpas-ui/components-ui/components/Icon";
import "./_style.less";
import { getCurrentSysName, getHbTypeByZcbfid, MergeType, } from "../../../../tools/Tools";
const { Option } = Select;
/**
 * 判断数组中的名称是否有重复
 * @param array
 * @returns
 */
function hasDuplicateNames(arr) {
    const nameSet = {};
    for (const item of arr) {
        if (item.rmap && item.type) {
            if (nameSet[item.rmap]) {
                return true; // 找到重复名称
            }
            nameSet[item.rmap] = true;
        }
    }
    return false; // 没有重复名称
}
// 设置行列标签弹窗
const RenderAddLabelPopOver = ({ _this, rowTagToolItemRef, columnTagToolItemRef, }) => {
    const tagInputRef = useRef(null);
    useEffect(() => {
        // _this.hot.deselectCell(); //行标签/行聚合标签保留单元格选择操作
    }, []);
    const handleOkClick = () => {
        let tagName;
        if (tagInputRef.current) {
            tagName = tagInputRef.current.input.value;
        }
        if (trim(tagName)) {
            _this.setTag(_this.state.tagType, tagName);
        }
        _this.setState({
            isOpenSetTag: false,
        });
    };
    const handleCancelClick = () => {
        _this.setState({
            isOpenSetTag: false,
        });
    };
    return (_jsxs(Popover, { target: _this.state.tagType === "row"
            ? rowTagToolItemRef.current
            : columnTagToolItemRef.current, position: "bottom", width: 350, height: "auto", visible: _this.state.isOpenSetTag, onHiding: _this.hideSetTagPanel, shading: true, shadingColor: "rgba(0, 0, 0, 0.5)", children: [_jsx("div", { style: { marginBottom: 10 }, children: _this.state.tagType === "row" ? " 设置行标签" : "设置列标签" }), _jsx("div", { children: _this.state.tagType === "row"
                    ? _this.state.currentRowTags.map((item, index) => {
                        return item ? (_jsx(Tag, { closable: true, color: "#2db7f5", style: { marginBottom: 6 }, onClose: (e) => {
                                //e.preventDefault();
                                _this.gridRef.tagService.handleDeleteRowLabel(item);
                            }, children: item }, index)) : null;
                    })
                    : _this.state.currentColumnTags.map((item, index) => {
                        return (item && (_jsx(Tag, { closable: true, color: "#2db7f5", style: { marginBottom: 6 }, onClose: (e) => {
                                //e.preventDefault();
                                _this.gridRef.tagService.handleDeleteColLabel(item);
                            }, children: item }, index)));
                    }) }), _jsx("div", { style: { marginTop: "10px" }, children: _jsx(Input, { placeholder: _this.state.tagType === "row" ? " 创建行标签" : "创建列标签", defaultValue: "", ref: tagInputRef, id: "rowTagInputId" }) }), _jsx("div", { style: { marginTop: 12, position: "relative", marginBottom: 40 }, children: _jsx("div", { style: { position: "absolute", right: 0 }, children: _jsxs(Space, { children: [_jsx(Button, { type: "primary", size: "small", onClick: handleOkClick, children: "\u786E\u5B9A" }), _jsxs(Button, { size: "small", onClick: handleCancelClick, children: [" ", "\u53D6\u6D88", " "] })] }) }) })] }));
};
// 设置行标签聚合弹窗
const renderLabelJuHePopOver = (_this, rowTagPolymericRef, columnTagPolymericRef) => {
    const handleJuHeOkClick = () => {
        if (_this.state.currentSelectedTags.length <= 0) {
            _this.setState({
                isOpenPolymeric: false,
            });
            return;
        }
        if (_this.state.tagType === "row") {
            _this.setRowCountType(_this.state.pType, _this.state.currentSelectedTags);
        }
        else {
            _this.setColumnCountType(_this.state.pType, _this.state.currentSelectedTags);
        }
        _this.setState({
            isOpenPolymeric: false,
        });
    };
    const handleJuHeCancelClick = () => {
        _this.setState({
            isOpenPolymeric: false,
        });
    };
    return (_jsxs(Popover, { target: _this.state.tagType === "row"
            ? rowTagPolymericRef.current
            : columnTagPolymericRef.current, position: "bottom", width: 350, height: "auto", visible: _this.state.isOpenPolymeric, onHiding: _this.hideLabelJuHePanel, shading: true, shadingColor: "rgba(0, 0, 0, 0.5)", closeOnOutsideClick: false, children: [_jsx("div", { style: { marginBottom: 10 }, children: _this.state.tagType === "row" ? "设置聚合行" : "设置聚合列" }), _jsxs("div", { children: [_jsx("div", { style: { marginBottom: 10 }, children: "\u805A\u5408\u65B9\u5F0F" }), _jsx(SelectBox, { placeholder: "", displayExpr: "name", valueExpr: "id", items: _this.juHeFangShiData, value: _this.state.pType, className: "select-box-style-min", onItemClick: (e) => {
                            _this.juHeFangShiChange(e);
                        } })] }), _jsxs("div", { children: [_jsx("div", { style: { marginTop: 10, marginBottom: 10 }, children: "\u9009\u62E9\u6807\u7B7E:" }), _jsx(Select, { mode: "multiple", allowClear: true, style: { width: "100%" }, placeholder: "\u8BF7\u9009\u62E9\u6807\u7B7E", defaultValue: [], value: _this.state.currentSelectedTags, onChange: _this.selectTagsChange, children: _this.state.tagType === "row"
                            ? _this.state.allRowTags.map((item, index) => {
                                return (_jsx(Option, { value: item, children: item }, index));
                            })
                            : _this.state.allColumnTags.map((item, index) => {
                                return (_jsx(Option, { value: item, children: item }, index));
                            }) })] }), _jsx("div", { style: { marginTop: 12, position: "relative", marginBottom: 40 }, children: _jsx("div", { style: { position: "absolute", right: 0 }, children: _jsxs(Space, { children: [_jsx(Button, { type: "primary", size: "small", onClick: handleJuHeOkClick, children: "\u786E\u5B9A" }), _jsx(Button, { size: "small", onClick: handleJuHeCancelClick, children: "\u53D6\u6D88" })] }) }) })] }));
};
//设置自定义参与运算
const renderCustomOperation = (_this, rowCustomOperationRef) => {
    const handleOperationOkClick = () => {
        const currentOperationLists = _this.state.currentOperationLists;
        if (!_this.gridRef.state.currentRecord) {
            _this.setState({
                isOpenCustomOperation: false,
            });
            return;
        }
        const { row } = _this.gridRef.state.currentRecord.coords;
        let cwCfg = {};
        let isRepeat = hasDuplicateNames(currentOperationLists);
        if (isRepeat) {
            message.open({
                type: "warning",
                content: "请注意！设置的行标签有重复，只有最后一个生效～",
                className: "custom-class",
                style: {
                    marginTop: "20vh",
                },
            });
        }
        for (let i = 0; i < currentOperationLists.length; i++) {
            const { rmap, type } = currentOperationLists[i];
            if (rmap && type) {
                cwCfg[rmap] = {
                    [type]: true,
                };
            }
        }
        if (JSON.stringify(cwCfg) !== "{}") {
            _this.gridService.setRowConfig(row, {
                cw: cwCfg,
            });
        }
        else {
            //删除cw配置
            const gridData = _this.gridRef.data;
            const lastHeader = _this.headerService.lastHeader;
            const config = JSON.parse(gridData[row][lastHeader]);
            if (config.cw) {
                delete config.cw;
                gridData[row][lastHeader] = JSON.stringify(config);
                _this.hot.updateData(gridData);
            }
        }
        console.log(_this.hot.getData(), "data");
        _this.setState({
            isOpenCustomOperation: false,
        });
    };
    const handleOperationCancelClick = () => {
        _this.setState({
            currentOperationLists: [],
            isOpenCustomOperation: false,
        });
    };
    const handleOperationDelClick = (e, index) => {
        let newCurrentOperationLists = _this.state.currentOperationLists;
        newCurrentOperationLists.splice(index, 1);
        _this.setState({
            currentOperationLists: [...newCurrentOperationLists],
        });
    };
    const getTagLists = () => {
        const data = _this.gridRef.data;
        let tagList = [];
        if (!data || !data.length) {
            return [];
        }
        for (let i = 0; i < data.length; i++) {
            if (i > 0) {
                const info = _this.formulaService.getPolymerizationInfoByRow(i);
                if (info) {
                    const rmap = data[i][0];
                    tagList.push({
                        id: rmap,
                        value: rmap,
                        name: rmap,
                    });
                }
            }
        }
        return tagList;
    };
    const dataColumns = [
        {
            title: "行标签",
            dataIndex: "rmap",
            key: "rmap",
            width: 200,
            render: (_, record, index) => (_jsx(React.Fragment, { children: _jsx(Select, { placeholder: "\u8BF7\u9009\u62E9\u884C\u6807\u7B7E", options: getTagLists(), style: { width: 190 }, className: "select_type", defaultValue: record.rmap, onChange: (e) => {
                        record.rmap = e;
                    } }, record.rmap) })),
        },
        {
            title: "权重类型",
            dataIndex: "type",
            key: "type",
            width: 150,
            render: (_, record, index) => (_jsx(React.Fragment, { children: _jsx(Select, { placeholder: "\u8BF7\u9009\u62E9\u8FD0\u7B97\u7C7B\u578B", options: _this.customOperationData, className: "select_type", defaultValue: record.type, onChange: (e) => {
                        record.type = e;
                    } }, record.type) })),
        },
        {
            title: "操作",
            width: 60,
            dataIndex: "state",
            key: "state",
            render: (_, record, index) => (_jsx(React.Fragment, { children: _jsx("div", { className: "icon-box", onClick: (e) => handleOperationDelClick(e, index), children: _jsx(CpasIcon, { type: "icon-remove", className: "cpas-icon" }) }) })),
        },
    ];
    return (_jsx(Popover, { target: rowCustomOperationRef.current, position: "bottom", width: 470, height: 300, visible: _this.state.isOpenCustomOperation, onHiding: _this.hideLabelCustomOperation, shading: true, shadingColor: "rgba(0, 0, 0, 0.5)", closeOnOutsideClick: false, children: _jsxs("div", { className: "operation-popover", children: [_jsx("div", { className: "operation-add-box", children: _jsxs("div", { className: "operation-add", onClick: _this.addCustomOperation, children: [_jsx(CpasIcon, { type: "icon-add-circle1", className: "cpas-icon" }), "\u65B0\u589E"] }) }), _jsx("div", { className: "operation-table", children: _jsx(Table, { className: "table-up", columns: dataColumns, dataSource: _this.state.currentOperationLists, pagination: false, 
                        // rowKey={(record) => record.number}
                        scroll: { y: 150 } }) }), _jsxs("div", { className: "operation-content-bottom", children: [_jsx("div", { className: "operation-tip", children: "\u6CE8\u610F\uFF1A\u81EA\u5B9A\u4E49\u7684\u6743\u91CD\u5927\u4E8E\u4E2D\u3001\u51CF\u884C\u8BBE\u7F6E~" }), _jsx("div", { className: "operation-button", children: _jsxs(Space, { children: [_jsx(Button, { type: "primary", size: "small", onClick: handleOperationOkClick, children: "\u786E\u5B9A" }), _jsx(Button, { size: "small", onClick: handleOperationCancelClick, children: "\u53D6\u6D88" })] }) })] })] }) }));
};
const Editor = ({ _this }) => {
    const rowTagToolItemRef = useRef();
    const rowTagPolymericRef = useRef();
    const rowCustomOperationRef = useRef();
    const columnTagToolItemRef = useRef();
    const columnTagPolymericRef = useRef();
    const sysName = getCurrentSysName();
    const mergeType = getHbTypeByZcbfid();
    return (_jsxs(_Fragment, { children: [_this.state.isOpenSetTag && (_jsx(RenderAddLabelPopOver, { _this: _this, rowTagToolItemRef: rowTagToolItemRef, columnTagToolItemRef: columnTagToolItemRef })), renderLabelJuHePopOver(_this, rowTagPolymericRef, columnTagPolymericRef), renderCustomOperation(_this, rowCustomOperationRef), _jsx(TableEditor, { _this: _this }), (mergeType === MergeType.TEMPLATE || mergeType === MergeType.UNKNOW) && (_jsx(ColumnEditor, { _this: _this, columnTagPolymericRef: columnTagPolymericRef, columnTagToolItemRef: columnTagToolItemRef })), _jsx(RowEditor, { _this: _this, rowTagToolItemRef: rowTagToolItemRef, rowTagPolymericRef: rowTagPolymericRef, rowCustomOperationRef: rowCustomOperationRef })] }));
};
export default Editor;
